package com.example.idealtv;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.PermissionRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebChromeClient;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.idealtv.client.MWebChromeClient;
import com.example.idealtv.client.MWebViewClient;
import com.yzj.jinyimojv.R;

public class MetaHumenActivity extends Activity {
    private PermissionRequest myRequest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermission();
        setContentView(R.layout.activity_meta_humen);
        WebView webView = (WebView)findViewById(R.id.web_view);
        WebSettings settings = webView.getSettings();
        configWebview(settings);
        webView.setWebViewClient(new MWebViewClient());
        webView.setWebChromeClient(new MWebChromeClient(this){
            @Override
            public void onPermissionRequest(PermissionRequest request) {
                for (String permission : request.getResources()) {
                    switch (permission) {
                        case "android.webkit.resource.AUDIO_CAPTURE": {
                            askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
                            break;
                        }
                    }
                }
            }
        });
        webView.clearCache(true);
        webView.loadUrl("http://ideal2077.7766.org:81/");
    }

    private void configWebview(WebSettings wSet) {
        wSet.setJavaScriptEnabled(true);
        wSet.setDomStorageEnabled(true);
        wSet.setGeolocationEnabled(true);
        wSet.setDatabaseEnabled(true);
        wSet.setAllowFileAccess(true);
        wSet.setMediaPlaybackRequiresUserGesture(false);
        wSet.setUseWideViewPort(true);
        wSet.setLoadWithOverviewMode(true);
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        wSet.setDatabasePath(dir);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            wSet.setAllowFileAccessFromFileURLs(true);
            wSet.setAllowUniversalAccessFromFileURLs(true);
        }
    }
    private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 101;
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_RECORD_AUDIO: {
                Log.d("WebView", "PERMISSION FOR AUDIO");
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    myRequest.grant(myRequest.getResources());
                    WebView webView = (WebView)findViewById(R.id.web_view);
                    webView.loadUrl("http://ideal2077.7766.org:81/");

                } else {
                    WebView webView = (WebView)findViewById(R.id.web_view);
                    webView.loadUrl("http://ideal2077.7766.org:81/");
                }
            }
        }
    }
    public void askForPermission(String origin, String permission, int requestCode) {
        Log.d("WebView", "inside askForPermission for" + origin + "with" + permission);

        if (ContextCompat.checkSelfPermission(getApplicationContext(),
                permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(MetaHumenActivity.this,
                    permission)) {

            } else {
                ActivityCompat.requestPermissions(MetaHumenActivity.this,
                        new String[]{permission},
                        requestCode);
            }
        } else {
            myRequest.grant(myRequest.getResources());
        }
    }
    private static final int MY_PERMISSIONS_REQUEST = 1;
    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(MetaHumenActivity.this,
                Manifest.permission.MODIFY_AUDIO_SETTINGS) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(MetaHumenActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(MetaHumenActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(MetaHumenActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(MetaHumenActivity.this, new String[]{
                    Manifest.permission.MODIFY_AUDIO_SETTINGS,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, MY_PERMISSIONS_REQUEST);
        }
    }
}